//
//  LoginViewController.m
//  EyeCare
//
//  Created by 韩盼盼 on 2016/12/21.
//  Copyright © 2016年 HG. All rights reserved.
//

#import "LoginViewController.h"
#import "SiginViewController.h"
#import "ForgetPwdController.h"

#import "BindLoginViewController.h"
#import "MainTabbarConrtroller.h"
@interface LoginViewController ()<UITextFieldDelegate>
{
    UIView *contentView;
    UIImageView *loginTop;
    UITextField *usernameText;
    UITextField *userpassword;
    
    UIButton *loginBtn;
    
    UIView *bottomView;

    
}
@property (nonatomic,strong) NSMutableArray *mutable_checks;

@end

@implementation LoginViewController

-(NSMutableArray *)mutable_checks{
    if (!_mutable_checks) {
        _mutable_checks=[[NSMutableArray alloc]initWithObjects:@"0",@"0", nil];
    }
    return _mutable_checks;
}
- (void)viewDidLoad {
//    self.title = @"登录";
//    self.navBarType = YES;
    [super viewDidLoad];
    [self updateNav];
    [self updateNavi];
    self.view.backgroundColor = HGCOLOR(@"f7f7f7");
    [self initWithSubViews];



}

-(void)updateNav
{
//    [self.navigateBar.leftBtn setHidden:NO];
//    
//    [self.navigateBar.bgImageView setBackgroundColor:[UIColor whiteColor]];
//    [self.navigateBar.backImg setImage:[UIImage imageNamed:@"left_arrow_blue"]];
    [self setTitle:@"登录"];
    
//    self.navigateBar.titleLabel.text = @"登录";
//    self.navigateBar.titleLabel.textColor = RGBCOLOR(51, 51, 51);
//    
//    self.navigateBar.titleLabel.whc_CenterX(0).whc_Height(44).whc_TopSpace(20);
//    [self.navigateBar.lineImageView setHidden:NO];
}

-(void)updateNavi
{
//    [self.navigateBar.leftBtn addTarget:self action:@selector(leftbtnAction) forControlEvents:UIControlEventTouchUpInside];
}
-(void)leftbtnAction
{
    NSLog(@"%@",self.tabBarController.viewControllers);
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}
-(void)initWithSubViews{
    
    contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, __kScreenWidth, __kScreenHeight - 64)];
    contentView.backgroundColor = HGCOLOR(@"ffffff");
    [self.view addSubview:contentView];
    
    loginTop =[[UIImageView alloc]initWithFrame:CGRectMake((__kScreenWidth-HGADPT(100))/2, HGADPT(125), HGADPT(100), HGADPT(100))];
    loginTop.image =[UIImage imageNamed:@"icon180"];
    [contentView addSubview:loginTop];
    
    NSLog(@"__kScreenWidth----%fADPT------%f",__kScreenWidth,ADPT);
    
    usernameText = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, loginTop.bottom+HGADPT(130), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    usernameText.backgroundColor = HGCOLOR(@"ffffff");
    usernameText.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    usernameText.keyboardType = UIKeyboardTypeNumberPad;
    usernameText.delegate = self;
    [usernameText addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    usernameText.layer.borderWidth = HGADPT(1);
    usernameText.clipsToBounds = YES;
    usernameText.placeholder = @"请输入工号";
    usernameText.leftView = [self getLeftView:@"login_username"];
    usernameText.leftViewMode = UITextFieldViewModeAlways;
    [usernameText setValue:@"11" forKey:@"LimitInput"];
    usernameText.layer.cornerRadius = 10 * ADPT;
    usernameText.tintColor = BTN_NORMAL_COLOR;
    usernameText.font = HGFont(30);
    usernameText.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [contentView addSubview:usernameText];
    
    
    userpassword = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, usernameText.bottom+HGADPT(40), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    userpassword.placeholder =@"请输入6~12位字符的密码组合";
    userpassword.tintColor = BTN_NORMAL_COLOR;
    userpassword.backgroundColor = HGCOLOR(@"ffffff");
    userpassword.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    userpassword.keyboardType = UIKeyboardTypeASCIICapable;

    userpassword.layer.borderWidth = HGADPT(1);
    userpassword.clipsToBounds = YES;
    userpassword.font = HGFont(30);
    userpassword.delegate  =self;
    [userpassword addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    userpassword.leftView = [self getLeftView:@"login_userpwd"];
    userpassword.clearButtonMode = UITextFieldViewModeUnlessEditing;
    userpassword.secureTextEntry = YES;
    userpassword.leftViewMode = UITextFieldViewModeAlways;
    [userpassword setValue:@"16" forKey:@"LimitInput"];
    userpassword.layer.cornerRadius = 10 * ADPT;
    [contentView addSubview:userpassword];
    
    loginBtn =[[UIButton alloc]initWithFrame:CGRectMake(MARGIN_LEFT, userpassword.bottom+HGADPT(58), __kScreenWidth - 2*MARGIN_LEFT, HGADPT(98))];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:HGCOLOR(@"ffffff") forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 10 * ADPT;
    loginBtn.titleLabel.font = HTFont(36 * ADPT);
    loginBtn.backgroundColor = HGColor(221, 221, 221);
    loginBtn.userInteractionEnabled = NO;
    [loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:loginBtn];
    
    UIView *loginView =[[UIView alloc]initWithFrame:CGRectMake(MARGIN_LEFT, loginBtn.bottom + HGADPT(50), __kScreenWidth - 2*MARGIN_LEFT, HGADPT(145))];
//    [contentView addSubview:loginView];
    
    UIImageView *typeimg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, loginView.width, HGADPT(23))];
    typeimg.image =[UIImage imageNamed:@"login_3f"];
//    [loginView addSubview:typeimg];
    
    
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, contentView.height - HGADPT(98), __kScreenWidth, HGADPT(98))];
    bottomView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:bottomView];
    
    NSArray *btnType = @[@"忘记密码？",@"注册"];
    CGFloat viewW = __kScreenWidth/2;
    [btnType enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(idx*viewW, HGADPT(0), viewW, HGADPT(98))];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:HGCOLOR(@"666666") forState:UIControlStateNormal];
        btn.backgroundColor = HGCOLOR(@"ffffff");
        btn.titleLabel.font = HTFont(30 * ADPT);
        [bottomView addSubview:btn];
        btn.tag = 20+idx;
        [btn addTarget:self action:@selector(BtnTypeAction:) forControlEvents:UIControlEventTouchUpInside];

    }];
    
    UIImageView *lineImg = [UIImageView new];
    lineImg.backgroundColor = HGCOLOR(@"999999");
    [bottomView addSubview:lineImg];
    lineImg.whc_Width(2 * ADPT).whc_Height(30 * ADPT).whc_CenterX(0).whc_CenterY(0);
}

//竖屏
-(void)setVerticalFrame{
    usernameText.frame =  CGRectMake(MARGIN_LEFT, loginTop.bottom+HGADPT(130), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98));
    userpassword.frame = CGRectMake(MARGIN_LEFT, usernameText.bottom+HGADPT(40), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98));
    
    loginBtn.frame = CGRectMake(MARGIN_LEFT, userpassword.bottom+HGADPT(58), __kScreenWidth - 2*MARGIN_LEFT, HGADPT(98));
    
    bottomView.frame = CGRectMake(0, contentView.height - HGADPT(98), __kScreenWidth, HGADPT(98));
}

//横屏
-(void)setHorizontalFrame{
    
    
    
}

-(UIView *)getLeftView:(NSString *)imgstr{
    UIView *leftView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, HGADPT(114), HGADPT(98))];
    UIImage *image=[UIImage imageNamed:imgstr];
    
    UIImageView *lefticon=[[UIImageView alloc]initWithFrame:CGRectMake(HGADPT(43),(leftView.height- HGADPT(image.size.height))/2 , HGADPT(image.size.width), HGADPT(image.size.height))];
    [leftView addSubview:lefticon];
    lefticon.image =image;
    return leftView;
}


-(void)BtnTypeAction:(UIButton *)btn{
    if (btn.tag == 20) {
        ForgetPwdController *siginVc =[[ForgetPwdController alloc]init];
        [self.navigationController pushViewController:siginVc animated:YES];
    }
    
    if (btn.tag == 21) {
        SiginViewController *siginVc =[[SiginViewController alloc]init];
        [self.navigationController pushViewController:siginVc animated:YES];

    }
}

-(void)textFieldEditingChanged:(UITextField *)textField
{
    if (usernameText == textField) {
        if (usernameText.text.length>10) {
            if ([HGDataCheck isMobileNumber_str:usernameText.text]) {
                [self checkMobile];
                [self.mutable_checks replaceObjectAtIndex:0 withObject:@"1"];
                
            }
            else{
                [MBProgressHUD showSuccess:@"请输入正确的手机号！"];
                [self.mutable_checks replaceObjectAtIndex:0 withObject:@"0"];
                
            }
        }
        else
        {
            loginBtn.backgroundColor = HGColor(221, 221, 221);
            loginBtn.userInteractionEnabled = NO;
        }
    }
    if (userpassword == textField) {
        if (textField.text.length>5&&textField.text.length<12) {
            [self.mutable_checks replaceObjectAtIndex:1 withObject:@"1"];
            
        }
        else
        {
            [self.mutable_checks replaceObjectAtIndex:1 withObject:@"0"];
            loginBtn.backgroundColor = HGColor(221, 221, 221);
            loginBtn.userInteractionEnabled = NO;
        }
    }
    
    [self checkInputContent];
    
}
-(void)checkInputContent
{
    NSLog(@"mutable_checks:::::%@",self.mutable_checks);
    
    if ([self.mutable_checks containsObject:@"0"]) {
        loginBtn.backgroundColor = HGColor(221, 221, 221);
        loginBtn.userInteractionEnabled = NO;
        
    }
    else
    {
        loginBtn.backgroundColor = app_base_color;
        loginBtn.userInteractionEnabled = YES;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"编辑结束！");
    if (usernameText == textField) {
        if (textField.text.length<10) {
            [self.mutable_checks replaceObjectAtIndex:0 withObject:@0];
            [MBProgressHUD showSuccess:textField.text.length==0?@"手机号不能为空！":@"请输入正确的手机号！"];

        }
        
    }
    if (userpassword == textField) {
        if (textField.text.length<5) {
            [MBProgressHUD showSuccess:@"密码位数不够！"];
            [MBProgressHUD showSuccess:textField.text.length==0?@"密码不能为空！":@"密码位数不够！"];

        }
        if (textField.text.length >12) {
            [MBProgressHUD showSuccess:@"密码位数不能超出12位字符"];
            
        }
    }
}

-(void)saveUserInfo:(NSDictionary *)obj
{
    
//    [GVUserDefaults standardUserDefaults].userName = [obj objectForKey:@"name"];
//    [GVUserDefaults standardUserDefaults].mobile = [obj objectForKey:@"mobile"];
//    [GVUserDefaults standardUserDefaults].realname = [obj objectForKey:@"realname"];
//    [GVUserDefaults standardUserDefaults].headimg = [obj objectForKey:@"headimg"];
//    [GVUserDefaults standardUserDefaults].balance = [obj objectForKey:@"balance"];
//    [GVUserDefaults standardUserDefaults].sex = [obj objectForKey:@"sex"];
//    NSString *birthday = [NSString stringWithFormat:@"%@",[obj objectForKey:@"birth"]];
//    if (IsAvailableString(birthday)) {
//        [GVUserDefaults standardUserDefaults].birthday = birthday;
//    }else
//    {
//        [GVUserDefaults standardUserDefaults].birthday = @"";
//    }
//    
//    NSString *signature = [NSString stringWithFormat:@"%@",[obj objectForKey:@"signature"]];
//    if (IsAvailableString(signature)) {
//        [GVUserDefaults standardUserDefaults].signature = [obj objectForKey:@"signature"];
//    }else
//    {
//        [GVUserDefaults standardUserDefaults].signature = @"";
//    }
//    
//    if (!IsAvailableString([GVUserDefaults standardUserDefaults].userName)) {
//        LAddUserInfoViewController *next = [[LAddUserInfoViewController alloc] init];
//        [self.navigationController pushViewController:next animated:YES];
//        return;
//    }
//    [GVUserDefaults standardUserDefaults].isLogin = YES;
//    [GVUserDefaults standardUserDefaults].isverified = [obj objectForKey:@"isverified"];
//    
//    [[NSNotificationCenter defaultCenter] postNotificationName:USER_LOGIN_SUCCESSED object:nil];
//    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 检测手机号！
-(void)checkMobile
{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:usernameText.text forKey:@"mobile"];
//    
//    NSString *url = API_LHF_BASE_URL(MEMBER_MOBILE_CHECK);
//    
//    WS(ws);
//    
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypeGet parameters:params success:^(id obj) {
//        BOOL isexist = [[obj objectForKey:@"isexist"] boolValue];
//        if (!isexist) {
//            [ws.view makeToast:@"改手机号尚未注册，请前往注册!"];
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];

    
}


-(void)loginBtnAction:(UIButton *)btn
{
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"user_login_successed" object:nil];
//    [GVUserDefaults standardUserDefaults].isLogin = YES;
//    [self.navigationController popViewControllerAnimated:YES];
    
    [self loginAction];
//    [HGDataManager getInstance].isLogin = YES;
//    [self.navigationController popViewControllerAnimated:YES];
}


-(void)loginAction
{
    /*
     name	√	string	用户名称
     password	√	string	密码，sha1加密过的
     */
    
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:usernameText.text forKey:@"name"];
//    [params setObject:[userpassword.text sha1] forKey:@"password"];
//    
//    NSString *url = API_LHF_BASE_URL(MEMBER_LOGIN);
//    
//    WS(ws);
//    
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypePost parameters:params success:^(id obj) {
//        [GVUserDefaults standardUserDefaults].token = [obj objectForKey:@"token"];
//        [GVUserDefaults standardUserDefaults].failuretime = [obj objectForKey:@"failuretime"];
//        [ws getUserInfo];
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
    
    UIWindow * mainWindow = [[UIApplication sharedApplication].delegate window] ;
    mainWindow.rootViewController = [[MainTabbarConrtroller alloc]init];

}



-(void)getUserInfo
{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:[GVUserDefaults standardUserDefaults].token forKey:@"token"];
//    
//    NSString *url = API_LHF_BASE_URL(MEMBER_INFO);
//    
//    WS(ws);
//    
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypeGet parameters:params success:^(id obj) {
//        if ([obj isKindOfClass:[NSDictionary class]]) {
//            [ws saveUserInfo:obj];
//        }else
//        {
//            [ws.view makeToast:@"数据类型异常！"];
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
}




@end
